<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $helpers = array('Html', 'Form');
	var $components = array('Mailer');

	function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('register', 'confirm');
	}

	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid User.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function register() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The User has been saved', true));
				$this->login();
//				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid User', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {

			/**
			 * check if the email address as changed, reset the 'email_confirmed' field
			 */
			 $this->User->id = $this->data['User']['id'];
			 $old_email = $this->User->field('email');
			 if ($old_email != $this->data['User']['email']) {
			 	$this->data['User']['email_confirmed'] = '0';
			 }

			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The User has been saved', true));
				$this->redirect(array('action'=>'profile'));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for User', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->del($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

	function profile() {
		$this->redirect(array('action'=>'edit', $this->Auth->user('id')));
	}

	function login() {

		if ($this->Auth->user()) {
			$args = array();
			$args['pingKey'] = 'cashebo';
			$args['url'] = Router::url('/', true);
			$args = http_build_query($args);
			fopen('http://ebocash.ebotunes.com/ping/?'.$args, 'r');
			$this->redirect($this->Auth->loginRedirect);
		}

	}

	function logout() {
		$this->redirect($this->Auth->logout());
	}

	function getconfirm() {
		$m = $this->Mailer;

		$email = $this->Auth->user('email');
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
		$length = 32;
		$char_count = strlen($chars);

		$confirm_code = '';
		for($i=0; $i<$length; $i++) {
			$confirm_code .= substr($chars, rand(0, $char_count), 1);
		}

		$this->User->id = $this->Auth->user('id');
		$this->User->saveField('confirm_code', $confirm_code);

		$body = "Click on the link below to confirm your email address \n\n";
		$body .= Router::url(array('controller'=>'users', 'action'=>'confirm', $email, $confirm_code), true);
		$body .= "\n\n";
		$body .= "Thanks,\nebo";

		$m->AddAddress($email);
		$m->Subject = 'Please confirm your email address';
		$m->Body = $body;

		if ($m->Send()) {
			$this->set('sent', true);
		} else {
			$this->set('sent', false);
		}

//		$this->log($body);

	}

	function confirm($email, $code) {

		$user = $this->User->findByEmail($email);

		if ($user['User']['confirm_code'] == $code) {
			$this->User->id = $user['User']['id'];
			$this->User->saveField('email_confirmed', 1);
			$this->User->saveField('confirm_code', null);
			$this->Session->setFlash('Email address confirmed succesfully!');
		} else {
			$this->Session->setFlash('Error with email confirmation');
		}

	}

}
?>
